Skip to content

Customize log filename via pattern tokens in ZEL_LOADER_LOG_FILE - #499

Open
MaciejPlewka wants to merge 8 commits into
oneapi-src:masterfrom
MaciejPlewka:loggerName
Open

Customize log filename via pattern tokens in ZEL_LOADER_LOG_FILE#499
MaciejPlewka wants to merge 8 commits into
oneapi-src:masterfrom
MaciejPlewka:loggerName

Conversation

@MaciejPlewka

Copy link
Copy Markdown

No description provided.

Signed-off-by: Maciej Plewka <maciej.plewka@intel.com>
Signed-off-by: Maciej Plewka <maciej.plewka@intel.com>
@rwmcguir

rwmcguir commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

We have unit tests in the repo to test portions of this, perhaps you can add one to verify this operates?
You can ask AI to generate these, and put them as part of the CMakeFile in the ./test/ folder to execute with proper environment flags enabled. The body of the code looks solid.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds runtime expansion for ZEL_LOADER_LOG_FILE so log filenames can include per-process/per-run identifiers (PID, process name, timestamp), and documents the new behavior in the README.

Changes:

  • Implemented filename pattern expansion for %P (pid), %N (process name), %T (timestamp) when resolving ZEL_LOADER_LOG_FILE.
  • Added process-name sanitization and path basename extraction helpers used by the filename expander.
  • Updated README to describe filename token support and provide examples.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
source/utils/ze_logger.cpp Adds token expansion and supporting helpers; integrates resolved filename into logger creation and advanced-mode config output.
README.md Documents ZEL_LOADER_LOG_FILE token behavior and examples.
Comments suppressed due to low confidence (2)

source/utils/ze_logger.cpp:134

  • expandLogFilePattern() computes PID, process name (/proc/self/exe readlink), and timestamp even when the filename contains no '%' tokens (e.g., the default "ze_loader.log"). This adds avoidable overhead and extra syscalls during logger creation. Add a fast-path to return the pattern unchanged when it contains no '%' at all.
std::string expandLogFilePattern(const std::string &pattern) {
    if (pattern.empty()) {
        return pattern;
    }

source/utils/ze_logger.cpp:146

  • createLogger() and comments mention supporting a literal percent token ("%%"), but expandLogFilePattern() currently treats "%%" as two characters (it doesn't collapse to a single '%'). If you intend to support "%%" as an escape for a literal percent, handle it explicitly in the token switch.
    for (std::size_t i = 0; i < pattern.size(); ++i) {
        if (pattern[i] == '%' && i + 1 < pattern.size()) {
            switch (pattern[i + 1]) {
                case 'P':
                    expanded += pid;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +115 to +128
std::string startupTimestampForFileName() {
const auto now = std::chrono::system_clock::now();
const auto now_t = std::chrono::system_clock::to_time_t(now);
std::tm tm_buf{};
#ifdef _WIN32
localtime_s(&tm_buf, &now_t);
#else
localtime_r(&now_t, &tm_buf);
#endif

char timestamp[32] = {};
std::strftime(timestamp, sizeof(timestamp), "%Y%m%d-%H%M%S", &tm_buf);
return timestamp;
}
Comment thread README.md Outdated
Comment on lines +96 to +101
Supported filename pattern tokens:
- `%P` — process id
- `%N` — process executable base name
- `%T` — logger startup timestamp formatted as `YYYYMMDD-HHMMSS`

A `%` not followed by `P`, `N`, or `T` is kept verbatim in the filename.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Suppressed comments (3)

test/ze_logger_filename_pattern_unit_tests.cpp:81

  • Test name uses "ThenReturned..." while the rest of this file consistently uses "ThenReturns...". Renaming improves consistency and makes searches/filters more predictable.
TEST(ZeLoggerExpandLogFilePattern, GivenNoTokenThenReturnedUnchanged) {

source/utils/ze_logger.cpp:149

  • expandLogFilePattern() recomputes pid/process name/timestamp on every call and reserves capacity without accounting for the timestamp, which adds avoidable syscalls/allocations and can make %T vary across multiple expansions within the same process. Cache these values once per process and include timestamp.size() in the reserve calculation.
    const std::string pid = std::to_string(static_cast<long long>(GET_PID()));
    const std::string process_name = currentProcessName();
    const std::string timestamp = startupTimestampForFileName();

    std::string expanded;

test/ze_logger_filename_pattern_unit_tests.cpp:63

  • Test name uses "ThenReturned..." while the rest of this file consistently uses "ThenReturns...". Renaming improves consistency and makes searches/filters more predictable.

This issue also appears on line 81 of the same file.

TEST(ZeLoggerSanitizeFileNameComponent, GivenPlainNameThenReturnedUnchanged) {

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Suppressed comments (1)

source/utils/ze_logger.cpp:14

  • <cctype> is included but not used anywhere in this file; it can be removed to avoid unnecessary headers and potential lint warnings.
#include <cerrno>
#include <cctype>
#include <chrono>

Signed-off-by: Maciej Plewka <maciej.plewka@intel.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Suppressed comments (2)

source/utils/ze_logger.cpp:694

  • expandLogFilePattern() can trigger readlink()/time/getpid() when ZEL_LOADER_LOG_FILE contains tokens, but this is executed before checking whether logging is enabled or whether console logging is selected. That adds avoidable syscalls in the !logging_enabled fast path and when ZEL_LOADER_LOG_CONSOLE=1 (file name is unused). Consider only expanding when file logging will actually be used.
    // Expand filename pattern tokens (%P, %N, %T, %%) within ZEL_LOADER_LOG_FILE.
    // A filename without tokens is returned unchanged, preserving existing behaviour.
    std::string resolved_loader_file = expandLogFilePattern(loader_file);
    if (resolved_loader_file.empty()) {
        resolved_loader_file = loader_file;

source/utils/ze_logger.cpp:149

  • %P is cached in a function-local static const std::string. After fork(), the child inherits that cached value, so %P can incorrectly expand to the parent PID. Since %P is intended to disambiguate per-process logs, compute the PID per call (or otherwise detect PID changes) instead of caching it permanently.

This issue also appears on line 690 of the same file.

    // The token values are process-invariant, so compute them once (thread-safe
    // function-local statics) rather than issuing the pid/exe-path/time syscalls
    // on every call. Caching also gives %T a stable "logger startup" timestamp
    // across expansions instead of drifting between calls.
    static const std::string pid = std::to_string(static_cast<long long>(GET_PID()));
    static const std::string process_name = currentProcessName();
    static const std::string timestamp = startupTimestampForFileName();

Signed-off-by: Maciej Plewka <maciej.plewka@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants